﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web;

public class FileWatcher
{   
    private FileSystemWatcher Watcher = null;
    public FileWatcher(FileSystemWatcher Watcher) { 
        this.Watcher = Watcher;
    }

    public void DownloadFile(string Filename)
    {
        try
        {
            string[] Contents = File.ReadAllText(Filename).Split('|');
            File.Delete(Filename);
            Uri downloadUrl = new Uri(Contents[2], UriKind.RelativeOrAbsolute);
            FileInfo info = new FileInfo(Contents[0] + downloadUrl.Segments.GetValue(downloadUrl.Segments.Length - 1).ToString());

            string targetDir = info.DirectoryName;
            string targetFilename = info.Name.TrimEnd(info.Extension.ToCharArray());
            string targetFileExtension = info.Extension;
            WebClient client = new WebClient();
            byte[] data = client.DownloadData(downloadUrl);

            string zipname = targetFilename + DateTime.Now.ToString("yyyyMMddhhmmssffftt") + ".zip";
            Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(targetDir + @"\" + zipname);
            zip.AddEntry(targetFilename + targetFileExtension, data);
            zip.Save();

            SendMail(Contents, zipname);
        }
        catch (Exception ex)
        {
            SendErrorMail(Filename, ex.Message);
        }        
    }

    public void SendErrorMail(string filename, string msg)
    {
        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential("noreply@praveenm.co.in", "noreply");
            smtp.Host = "smtp1r.cp.365techsupport.com";
            smtp.Port = 25;
            smtp.EnableSsl = false;
            MailMessage message = new MailMessage();
            message.From = new MailAddress("noreply@praveenm.co.in");
            message.To.Add("mynewtestid@hotmail.com, praveen.mantapam@live.com");
            message.Subject = "Error..!";
            message.IsBodyHtml = true;
            message.Body = "<h4><b>" + msg + "</b></h4><h4>" + filename + "</h4>";


            smtp.Send(message);
        }
        catch (Exception ex) { }
    }

    public void SendMail(string[] Contents, string filename)
    {
        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential("noreply@praveenm.co.in", "noreply");
            smtp.Host = "smtp1r.cp.365techsupport.com";
            smtp.Port = 25;
            smtp.EnableSsl = false;
            MailMessage message = new MailMessage();
            message.From = new MailAddress("noreply@praveenm.co.in");
            message.To.Add(Contents[3]);
            message.Subject = "Your Download URL..!";
            message.IsBodyHtml = true;
            message.Body = "<h4><b><a href='" + Contents[1] + filename + "'>" + Contents[1] + filename + "</a></b></h4>";
            
            
            smtp.Send(message);
        }
        catch (Exception ex) { }
    }
}